package com.topica.network.download;

import com.topica.R;
import com.topica.network.callback.OnUnzipListener;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class UnzipTask extends AsyncTask<String, Void, Integer>{

	private ProgressDialog mProgressDialog;
	private Context context;
	private String fileName;
	private OnUnzipListener unzipListener;
	
	public UnzipTask(Context context, String fileName){
		this.context = context;
		this.fileName = fileName;
	}
	
	public void setUnzipListener(OnUnzipListener l){
		this.unzipListener = l;
	}
	
	@Override
	protected void onPostExecute(Integer result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		mProgressDialog.dismiss();
		if(result == 1){
			unzipListener.onUnzipSuccess(fileName);
		} else {
			unzipListener.onUnzipFailed();
		}
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		mProgressDialog = new ProgressDialog(context);
		mProgressDialog.setCancelable(false);
		mProgressDialog.setMessage(context.getResources().getString(R.string.unzipping));
		mProgressDialog.show();
	}

	@Override
	protected Integer doInBackground(String... params) {
		// TODO Auto-generated method stub
		Decompress dc = new Decompress(params[0] + fileName, params[0]);
		try {
			dc.unzip();
		} catch (Exception e){
			return 0;
		}
		return 1;
	}

}
